package com.sgb.kjwl.view.widget;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.self.SelfEmploymentEditEntity;
import com.swgk.core.util.MToast;
import com.swgk.core.util.ResolutionUtil;

import java.lang.reflect.Field;
import java.util.HashMap;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;
import cn.sharesdk.wechat.utils.WechatClientNotExistException;

/**
 * @author liWei
 * @date 2020/11/10
 * description:工队-灵活用工详情分享
 */
public class PackerEmploymentShareDialog extends DialogFragment {

    private Platform platform;
    private SelfEmploymentEditEntity bean;
    private String title;

    public static PackerEmploymentShareDialog newInstance(SelfEmploymentEditEntity data, String url) {
        PackerEmploymentShareDialog dialog = new PackerEmploymentShareDialog();
        Bundle bundle = new Bundle();
        bundle.putSerializable("bean", data);
        bundle.putString("url", url);
        dialog.setArguments(bundle);
        return dialog;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NORMAL, R.style.DialogStyle);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fr_share_dialog_employmengt, container, false);
        bean = (SelfEmploymentEditEntity) getArguments().getSerializable("bean");
        if (bean != null) {
            initView(view);
        }

        if (!TextUtils.isEmpty(bean.getProjectName())) {
            title = bean.getProjectName();
        }else {
            title = "";
        }
        if (!TextUtils.isEmpty(bean.getWorkTypeName())) {
            title = title + bean.getWorkTypeName();
        }
        if (!TextUtils.isEmpty(bean.getSalary()+"")) {
            title = title + bean.getSalary();
        }
        if (!TextUtils.isEmpty(bean.getSalaryUnitName())) {
            title = title+ bean.getSalaryUnitName();
        }
        return view;
    }

    private void initView(View view) {
        view.findViewById(R.id.lin_wx_f).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ShareSDKContent(0);
            }
        });
        view.findViewById(R.id.lin_wx_p).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ShareSDKContent(1);
            }
        });
        view.findViewById(R.id.tv_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

//    private Platform platform;

    //    private void ShareSDKContent(int flag) {
    private void ShareSDKContent(int flag) {
        Bitmap Bmp = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
        Platform.ShareParams sp = new Platform.ShareParams();
        sp.setTitle(title);
        sp.setText(title);
        sp.setImageData(Bmp);
        sp.setUrl(getArguments().getString("url"));
        sp.setShareType(Platform.SHARE_WEBPAGE);
        switch (flag) {
            case 0:
                platform = ShareSDK.getPlatform(Wechat.NAME);
                break;
            case 1:
                platform = ShareSDK.getPlatform(WechatMoments.NAME);
                break;
        }

        platform.setPlatformActionListener(new PlatformActionListener() {
            @Override
            public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                Log.e("tag", "分享成功................");
                dismiss();
            }

            @Override
            public void onError(Platform platform, int i, Throwable throwable) {
                if (throwable instanceof WechatClientNotExistException) {
                    MToast.showToast(getContext(), "您未安装微信客户端，分享失败！");
                }
            }

            @Override
            public void onCancel(Platform platform, int i) {

            }
        });
        platform.share(sp);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        getDialog().getWindow().getAttributes().windowAnimations = R.style.Bottom_Dialog_Animation;
        getDialog().setCanceledOnTouchOutside(true);
    }

    @Override
    public void onStart() {
        super.onStart();

        Window window = getDialog().getWindow();
        window.setGravity(Gravity.BOTTOM);
        DisplayMetrics dpMetrics = new DisplayMetrics();
        window.getWindowManager().getDefaultDisplay().getMetrics(dpMetrics);
        WindowManager.LayoutParams p = window.getAttributes();

        p.dimAmount = 0.5f;
        p.width = dpMetrics.widthPixels;
        p.height = ResolutionUtil.dp2px(getActivity(), 250);
        window.setAttributes(p);
    }

    public void showAllowingLoss(FragmentManager manager, String tag) {
        try {
            Class cls = DialogFragment.class;
            Field mDismissed = cls.getDeclaredField("mDismissed");
            mDismissed.setAccessible(true);
            mDismissed.set(this, false);
            Field mShownByMe = cls.getDeclaredField("mShownByMe");
            mShownByMe.setAccessible(true);
            mShownByMe.set(this, true);
        } catch (Exception e) {
            show(manager, tag);
            return;
        }
        FragmentTransaction ft = manager.beginTransaction();
        ft.add(this, tag);
        ft.commitAllowingStateLoss();
    }

    @Override
    public void dismiss() {
        if (getFragmentManager() == null) return;
        super.dismissAllowingStateLoss();
    }

}
